using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Library.UserControls
{
    public partial class ucShowProcedureInfo : UserControl
    {
        Library.Objects.ProcedureInfo _procedure;

        //public event Library.OpenFormEventHandler OpenProcedure;

        //public ucShowProcedureInfo(Library.Objects.IProcedureInfo procedure, Library.OpenFormEventHandler openprocedure)
        //{
        //    InitializeComponent();
        //    if (OpenProcedure == null)
        //        OpenProcedure += openprocedure;

        //    Fill((Objects.ProcedureInfo)procedure);
        //}
        //public ucShowProcedureInfo(Library.OpenFormEventHandler openprocedure)
        //{
        //    InitializeComponent();
        //    if (OpenProcedure == null)
        //        OpenProcedure += openprocedure;
            
        //    //Fill(procedure);
        //}

        public ucShowProcedureInfo()
        {
            InitializeComponent();
        }

        public void Fill(Library.Objects.ProcedureInfo procedure)
        {
            _procedure = procedure;
            checkBox_StartFromTheLatestData.Checked = procedure.StartFromTheLatestData;
            label_DateOfLastData.Text = procedure.DateOfLastDataFound.ToString();
            label_DateOfLastData.Visible = checkBox_StartFromTheLatestData.Checked;

            linkLabel_Results.Text = procedure.ResultList.Count.ToString();

            this.panel_ProcedureDetails.Controls.Clear();
            comboBox_Procedure.Items.Clear();
            comboBox_Procedure.Items.AddRange(procedure.KnownVersionList.ToArray());
            if (comboBox_Procedure.Items.Count > 0) comboBox_Procedure.SelectedIndex = 0;

            label_Procedure.Text = string.Format("[{1}]{0}", procedure.Name, procedure.KnownVersionList.Count);
        }

        private void comboBox_Procedure_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox_Procedure.SelectedItem != null)
            {
                this.panel_ProcedureDetails.Controls.Clear();
                Control control = null;
                Point p = new Point(0, 0);
                foreach (Control tmpcontrol in this.Controls)
                {
                    if (tmpcontrol.GetType() == typeof(ucShowProcedure))
                        control = (ucShowProcedure)tmpcontrol;
                }
                if (control != null)
                {
                    ((ucShowProcedure)control).Fill(((Objects.ProcedureVersion)comboBox_Procedure.SelectedItem).Parent);
                }
                else
                {
                    control = new ucShowProcedure();
                    control.Location = p;
                    //((UserControl)control).AutoSize = false;
                    control.Anchor = AnchorStyles.None;
                    //control.Anchor |= AnchorStyles.Left;
                    //control.Anchor |= AnchorStyles.Right;
                    //control.Anchor |= AnchorStyles.Bottom;
                    control.Dock = DockStyle.Fill;
                    //control.BringToFront();
                    //p = new Point(0, control.Size.Height);
                    //this.panel_ProcedureDetails.Size = new Size(this.panel_ProcedureDetails.Size.Width, control.Size.Height);

                    ((ucShowProcedure)control).Fill(((Objects.ProcedureVersion)comboBox_Procedure.SelectedItem).Parent);
                    this.panel_ProcedureDetails.Controls.Add(control);
                    //this.panel_ProcedureDetails.Controls["ucShowProcedureInfoDetails"].Dock = DockStyle.Fill;


                    //((ucShowProcedure)this.panel_ProcedureDetails.Controls["ucShowProcedureInfoDetails"]).OpenProcedure += OpenProcedure;
                }
                //control = ((Objects.ProcedureVersion)comboBox_Procedure.SelectedItem).Parent.Show();
                //control.Location = p;
                ////control.Dock = DockStyle.Fill;
                ////control.BringToFront();
                //control.Anchor |= AnchorStyles.Left;
                //control.Anchor |= AnchorStyles.Right;
                //this.panel_ProcedureDetails.Controls.Add(control);
            }
        }

        private void checkBox_StartFromTheLatestData_CheckedChanged(object sender, EventArgs e)
        {
            if (_procedure.DateOfLastDataFound <= DateTime.MinValue)
                label_DateOfLastData.Visible = false;
            else
            {
                this._procedure.StartFromTheLatestData = checkBox_StartFromTheLatestData.Checked;

                label_DateOfLastData.Visible = checkBox_StartFromTheLatestData.Checked;
            }
        }

        private void linkLabel_Results_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _procedure.OpenResultFile();
            _procedure.OpenResultList();
            _procedure.OpenResultText();
            _procedure.OpenResultGrid();
        }
    }
}
